<template>
  <div class="home">
    <h1>商品列表</h1>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="c">
      <div class="top">
        <el-input
          placeholder="请输入内容"
          v-model="pages.query"
          class="input-with-select"
        >
          <el-button slot="append" @click='seach' icon="el-icon-search"></el-button>
        </el-input>
        <el-button class="btn" type="primary" @click="add">添加商品</el-button>
      </div>
      <div class="bottom">
        <!-- 表格 -->
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column prop="goods_name" width="180px" label="商品名称">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.add_time | filterstime }}
            </template>
          </el-table-column>
          <el-table-column prop="caozuo" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="theEditor(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="delet(scope.row.goods_id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pages.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="pages.pagesize"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getgoods, putgoodsid } from "@/Api";
export default {
  data() {
    return {
      input3: "",
      tableData: [],
      pages: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
    };
  },
  mounted() {},
  //   过滤器
  filters: {
    filterstime(val) {
      let times = new Date(val * 1000).toLocaleString();
      return times;
    },
  },
  methods: {
    seach(){
      this.getgood()
    },
    handleSizeChange(val) {
      console.log(val, "1111111"); //一页显示多少条
      this.pages.pagesize = val;
      this.getgood();
    },
    handleCurrentChange(val) {
      console.log(val, "22222"); //当前的页码
      this.pages.pagenum = val;
      this.getgood();
    },
    getgood() {
      let wys = {
        query: this.pages.query,
        pagenum: this.pages.pagenum,
        pagesize: this.pages.pagesize,
      };
      getgoods(wys).then((res) => {
        console.log(res);
        this.tableData = res.data.goods;
        this.total = res.data.total;
      });
    },
    add() {
      this.$router.push({ path: "/Classif" });
    },
    theEditor(e) {
      console.log(e.goods_id);

      let str = JSON.stringify(e);
      //       console.log(str);

      this.$router.push({ path: "/Classif", query: { str } });
      // this.$router.push({ path: "/categories", qurey: { goods_id:e.goods_id} });
    },
    delet(e) {
      console.log(e);

      putgoodsid(e).then((res) => {
        if (res.meta.status === 200) {
          this.$message({
            message: res.meta.msg,
            type: "success",
          });
          this.getgood();
        } else {
          this.$message({
            message: res.meta.msg,
            type: "error",
          });
        }
      });
    },
  },
  components: {},
  created() {
    this.getgood();
  },
};
</script>

<style lang='scss' scoped>
h1 {
  color: #fff;
}
.c {
  // background-color: #34429a;
  margin-top: 10px;
}
.input-with-select {
  width: 30%;
}
.top {
  display: flex;
  margin-bottom: 15px;
}
.btn {
  margin-left: 10px;
}
</style>
